<?php

namespace app\api\controller\v39;

use app\api\controller\v2\Auth;
use app\common\cachemodel\AppBlacklist;
use app\common\cachemodel\zl1_relation\Zl1AstrCommunityArticle;
use app\common\cachemodel\zlcr2\Zlcr2UserQuestion;
use app\common\lib\Util;
use app\common\model\User;

class Blacklist extends Auth
{
    public function getList()
    {
        $this->request->get(['page' => -1]);
        $appBlacklistModel = new AppBlacklist();

        $pageData = $appBlacklistModel->getListPage($this->user['id']);
        $data = array_column($pageData['data'], null, 'id');
        krsort($data);

        $resData = [];
        foreach ($data as $item)
        {
            $resData[] = [
                'id' => (int)$item['id'],
                'avatar' => $item['avatar'],
                'nickname' => $item['nickname'],
            ];
        }

        return show(config('code.success'), '请求成功', $resData);
    }

    public function add()
    {
        $type = input('type/d', 0); // 1=>问答   2=>星座讨论
        $id = input('id/s', 0);

        if (!in_array($type, [1, 2], true) || !$id)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        if (Util::commentCheck($this->user['id'], ['action' => 'add', 'type' => $type, 'id' => $id], 5))
        {
            return show(config('code.error'), '操作频繁，请稍后再试～');
        }

        if ($type === 1)
        {   //  1=>问答
            $questionModel = new Zlcr2UserQuestion();
            $dataInfo = $questionModel->getInfoById($id, ['uid', 'is_anonymity']);
            $uid = (int)$dataInfo['uid'];
            if ($dataInfo['is_anonymity'])
            {
                $nickname = '匿名用户';
                $avatar = '/test/question_answer/anonymity1.png';
            } else
            {
                $userInfo = (new User())->getUserinfo(['id' => $dataInfo['uid']]);
                $avatar = $userInfo['avatar'];
                $nickname = $userInfo['nickname'];
            }
        } else
        {   // 2=>星座讨论
            $astrCommunityArticleModel = new Zl1AstrCommunityArticle();
            $dataInfo = $astrCommunityArticleModel->getInfoById($id, ['uid']);

            $userInfo = (new User())->getUserinfo(['id' => $dataInfo['uid']]);

            $uid = (int)$dataInfo['uid'];
            $avatar = $userInfo['avatar'];
            $nickname = $userInfo['nickname'];
        }

        $appBlacklistModel = new AppBlacklist();
        $this->request->get(['page' => -1]);
        $pageData = $appBlacklistModel->getListPage($this->user['id']);

        foreach ($pageData['data'] as $item)
        {
            if ((int)$item['black_uid'] === $uid && $item['avatar'] === $avatar && $item['nickname'] === $nickname)
            {   // 用户已经加入黑名单 判定用户是否已在加入黑名单 同时也需要头像和昵称相同
                return show(config('code.success'), '添加成功');
            }
        }

        if ($pageData['total'] > 20)
        {
            return show(config('code.error'), '黑名单已满，请清理或者联系官方～');
        }

        $data = [
            'uid' => $this->user['id'],
            'black_uid' => $uid,
            'nickname' => $nickname,
            'avatar' => $avatar,
            'create_time' => $this->request->time(),
        ];
        $res = $appBlacklistModel->addData($data);
        if ($res)
        {
            return show(config('code.success'), '添加成功');
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }

    public function del()
    {
        $id = input('id/d', 0);

        $appBlacklistModel = new AppBlacklist();

        $dataInfo = $appBlacklistModel->getInfoById($id);

        if ($dataInfo)
        {
            $appBlacklistModel->delData($this->user['id'], $id);
        }
        return show(config('code.success'), '删除成功');
    }
}